#ifndef MESSAGESHOWAREA_H
#define MESSAGESHOWAREA_H

#include <QFileDialog>
#include <QMouseEvent>
#include <QScrollArea>
#include <QScrollBar>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QFontMetrics>
#include <QPainter>
#include <QPainterPath>
#include <QMenu>

#include "Debug.h"
#include "model/data.h"
#include "model/datacenter.h"
#include "userinfowidget.h"

using model::Message;
using model::MessageType;

class MessageShowArea : public QScrollArea
{
    Q_OBJECT
public:
    MessageShowArea();

    void addMessage(bool isLeft, const Message& msg);

    void addFrontMessage(bool isLeft, const Message& msg);

    void clear();

    void scrollToEndLater();    // 滚动到末尾
private:
    QWidget* m_container = nullptr;
};

////////////////////////////////////////////////////////////////////
///                            消息基类
////////////////////////////////////////////////////////////////////
class MessageItem: public QWidget{
    Q_OBJECT

public:
    MessageItem(bool isLeft, const Message &msg);

    static MessageItem* createMessageItem(bool isLeft, const Message& msg);

    QPushButton* getAvatarBtn() const { return m_avatar; }
    QLabel *getnameLabel() const { return m_nameLabel; }


private:
    static QWidget* createTextMessageItem(bool isLeft, const QByteArray &content);
    static QWidget* createImageMessageItem(bool isLeft, const QString& fileID, const QByteArray &content);
    static QWidget* createFileMessageItem(bool isLeft, const QString& fileID, const QString& fileName, const QByteArray &content);
    static QWidget *createSoundMessageItem(bool isLeft, const QString &fileID, const QByteArray &content);

private:
    bool m_isLeft = true;
    QPushButton* m_avatar = nullptr;
    QLabel* m_nameLabel = nullptr;
    QWidget* m_content = nullptr;
};

////////////////////////////////////////////////////////////////////////
///                             文字消息类
////////////////////////////////////////////////////////////////////////
class MessageContentLabel: public QWidget{
    Q_OBJECT
public:
    MessageContentLabel(bool isLeft, MessageType messageType, const QString& text, const QString& fileId, const QByteArray& content);

    void paintEvent(QPaintEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void contextMenuEvent(QContextMenuEvent* event) override;

private:
    void saveAsFile(const QByteArray& filePath);

private slots:
    void updateUI(const QString& fileID, const QByteArray &content);
    void speechConvertTextDone(const QString& fileID, const QString &text);

private:
    bool m_isLeft;
    QLabel* m_label;
    // 用于文件信息传输
    MessageType m_messageType;
    QString m_fileID;
    QByteArray m_content;
    bool m_loadContentDone;
};

////////////////////////////////////////////////////////////////////////
///                             图片消息类
////////////////////////////////////////////////////////////////////////
class MessageImageLabel: public QWidget{
    Q_OBJECT
public:
    MessageImageLabel(bool isLeft, const QString& fileID, const QByteArray& content);

    void paintEvent(QPaintEvent* event) override;

private slots:
    void updateUI(const QString& fileID, const QByteArray &content);

private:
    bool m_isLeft = false;
    QString m_fileID;
    QPushButton* m_imageBtn = nullptr;
    QByteArray m_content;
};

// class M
#endif // MESSAGESHOWAREA_H
